package com.ssy.lingxi.report.serviceimpl.tally;

import com.ssy.lingxi.report.api.enums.CommodityOperateTypeEnum;
import com.ssy.lingxi.report.api.enums.OperateDataSourceEnum;
import com.ssy.lingxi.report.api.vo.request.OperateNoticeVO;
import com.ssy.lingxi.report.dao.product.BrandDAO;
import com.ssy.lingxi.report.dao.product.CommodityDAO;
import com.ssy.lingxi.report.entity.report.MemberTallyReportDO;
import com.ssy.lingxi.report.model.bo.MemberTallyReportBO;
import com.ssy.lingxi.report.model.bo.tally.CommodityTallyBO;
import com.ssy.lingxi.report.model.bo.tally.statistics.CommodityStatisticsTallyBO;
import com.ssy.lingxi.report.model.vo.response.CommodityTallyReportVO;
import com.ssy.lingxi.report.model.vo.response.TallyReportItemVO;
import com.ssy.lingxi.report.service.tally.ICommodityTallyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品中心计数统计接口实现
 * @author 伍将
 * @date 2020/11/23
 * @version 2.0.0
 */
@Service
public class CommodityTallyServiceImpl implements ICommodityTallyService {

    private static final Logger logger = LoggerFactory.getLogger(LogisticsTallyServiceImpl.class);

    @Autowired
    private CommodityDAO commodityDAO;

    @Autowired
    private BrandDAO brandDAO;

    /**
     * 设置商品中心数据
     * @param tallyReportO
     * @param noticeVO
     * @return
     */
    @Override
    public MemberTallyReportBO setCommodityTally(MemberTallyReportBO tallyReportO, OperateNoticeVO noticeVO) {
        if (!noticeVO.getDataSource().equals(OperateDataSourceEnum.COMMODITY.getCode())) {
            return tallyReportO;
        }
        if (tallyReportO == null) {
            tallyReportO = new MemberTallyReportBO();
        }
        CommodityTallyBO commodity = tallyReportO.getCommodity();
        if (commodity == null) {
            commodity = new CommodityTallyBO();
        }
        // 更新数据
        CommodityOperateTypeEnum typeEnum;
        if (noticeVO.getReduceCount() > 0) {
            typeEnum = CommodityOperateTypeEnum.getEnum(noticeVO.getReduceOperateType());
            if (typeEnum != null) {
                switch (typeEnum) {
                    case PRODUCT_COUNT:
                        commodity.setProductCount(commodity.getProductCount() - noticeVO.getReduceCount());
                        break;
                    case BRAND_COUNT:
                        commodity.setBrandCount(commodity.getBrandCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_PRODUCT_VALIFY_COMMIT:
                        commodity.setToBeProductValifyCommitCount(commodity.getToBeProductValifyCommitCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_PRODUCT_PUBLISH:
                        commodity.setToBeProductPublishCount(commodity.getToBeProductPublishCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_BRAND_VALIFY_COMMIT:
                        commodity.setToBeBrandValifyCommitCount(commodity.getToBeBrandValifyCommitCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_BRAND_CHECK_COMMIT:
                        commodity.setToBeBrandValifyCount(commodity.getToBeBrandValifyCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_PRODUCT_CHECK_COMMIT:
                        commodity.setToBeProductValifyCount(commodity.getToBeProductValifyCount() - noticeVO.getReduceCount());
                        break;
                }
            }
        }
        if (noticeVO.getIncreaseCount() > 0) {
            typeEnum = CommodityOperateTypeEnum.getEnum(noticeVO.getIncreaseOperateType());
            if (typeEnum != null) {
                switch (typeEnum) {
                    case PRODUCT_COUNT:
                        commodity.setProductCount(commodity.getProductCount() + noticeVO.getIncreaseCount());
                        break;
                    case BRAND_COUNT:
                        commodity.setBrandCount(commodity.getBrandCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_PRODUCT_VALIFY_COMMIT:
                        commodity.setToBeProductValifyCommitCount(commodity.getToBeProductValifyCommitCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_PRODUCT_PUBLISH:
                        commodity.setToBeProductPublishCount(commodity.getToBeProductPublishCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_BRAND_VALIFY_COMMIT:
                        commodity.setToBeBrandValifyCommitCount(commodity.getToBeBrandValifyCommitCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_BRAND_CHECK_COMMIT:
                        commodity.setToBeBrandValifyCount(commodity.getToBeBrandValifyCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_PRODUCT_CHECK_COMMIT:
                        commodity.setToBeProductValifyCount(commodity.getToBeProductValifyCount() + noticeVO.getIncreaseCount());
                        break;
                }
            }
        }
        tallyReportO.setCommodity(commodity);

        return tallyReportO;
    }

    /**
     * 转换商品中心数据
     * @param reportDO
     * @return
     */
    @Override
    public CommodityTallyReportVO convertCommodityTally(CommodityTallyBO reportDO) {
        List<TallyReportItemVO> resultList = null;
        if (reportDO == null) {
            reportDO = new CommodityTallyBO();
        }
        resultList = new ArrayList<>();
        // 商品总数
        TallyReportItemVO tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(reportDO.getProductCount());
        tallyReportItem.setName(CommodityOperateTypeEnum.PRODUCT_COUNT.getName());
        tallyReportItem.setLink(CommodityOperateTypeEnum.PRODUCT_COUNT.getLink());
        resultList.add(tallyReportItem);
        // 品牌总数
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(reportDO.getBrandCount());
        tallyReportItem.setName(CommodityOperateTypeEnum.BRAND_COUNT.getName());
        tallyReportItem.setLink(CommodityOperateTypeEnum.BRAND_COUNT.getLink());
        resultList.add(tallyReportItem);
        // 待提交审核商品
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(reportDO.getToBeProductValifyCommitCount());
        tallyReportItem.setName(CommodityOperateTypeEnum.TO_BE_PRODUCT_VALIFY_COMMIT.getName());
        tallyReportItem.setLink(CommodityOperateTypeEnum.TO_BE_PRODUCT_VALIFY_COMMIT.getLink());
        resultList.add(tallyReportItem);
        // 待上架商品
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(reportDO.getToBeProductPublishCount());
        tallyReportItem.setName(CommodityOperateTypeEnum.TO_BE_PRODUCT_PUBLISH.getName());
        tallyReportItem.setLink(CommodityOperateTypeEnum.TO_BE_PRODUCT_PUBLISH.getLink());
        resultList.add(tallyReportItem);
        // 待提交审核品牌
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(reportDO.getToBeBrandValifyCommitCount());
        tallyReportItem.setName(CommodityOperateTypeEnum.TO_BE_BRAND_VALIFY_COMMIT.getName());
        tallyReportItem.setLink(CommodityOperateTypeEnum.TO_BE_BRAND_VALIFY_COMMIT.getLink());
        resultList.add(tallyReportItem);

        resultList.forEach(o -> o.setCount(o.getCount() > 0 ? o.getCount() : 0));

        CommodityTallyReportVO result = new CommodityTallyReportVO();
        result.setTallyList(resultList);

        return result;
    }

    /**
     * 统计计数
     * @param reportList
     * @return
     */
    @Override
    public List<MemberTallyReportDO> statisticsTally(List<MemberTallyReportDO> reportList) {
        // 商品
        reportList = statisticsCommodityTally(reportList);

        // 品牌
        reportList = statisticsBrandTally(reportList);

        return reportList;
    }

    /**
     * 统计品牌
     * @param reportList
     * @return
     */
    private List<MemberTallyReportDO> statisticsBrandTally(List<MemberTallyReportDO> reportList) {
        if (reportList == null) {
            reportList = new ArrayList<>();
        }
        List<CommodityStatisticsTallyBO> statisticsList = brandDAO.getStatisticsCount();
        if (statisticsList == null) {
            logger.info("没有符合条件的商品计数统计数据");
            statisticsList = new ArrayList<>();
        }

        // 补全数据
        List<MemberTallyReportDO> finalReportList = reportList;
        statisticsList.stream().distinct().forEach(p -> {
            MemberTallyReportDO reportDO = finalReportList.stream()
                    .filter(o -> o.getMemberId().equals(p.getMemberId()) && o.getRoleId().equals(p.getRoleId()))
                    .findFirst().orElse(null);
            if (reportDO == null) {
                reportDO = new MemberTallyReportDO();
                reportDO.setMemberId(p.getMemberId());
                reportDO.setRoleId(p.getRoleId());
                finalReportList.add(reportDO);
            }
        });

        List<CommodityStatisticsTallyBO> finalStatisticsList = statisticsList;
        finalReportList.forEach(p -> {
            // 如果报表数据为null，则实例化
            if (p.getTallyReport() == null) {
                p.setTallyReport(new MemberTallyReportBO());
            }
            CommodityTallyBO tallyBO = p.getTallyReport().getCommodity();
            if (tallyBO == null) {
                tallyBO = new CommodityTallyBO();
            }
            CommodityStatisticsTallyBO tallyVO = finalStatisticsList.stream().filter(o -> o.getMemberId().equals(p.getMemberId()) && o.getRoleId().equals(p.getRoleId())).findFirst().orElse(null);
            if (null != tallyVO) {
                // 品牌总数
                tallyBO.setBrandCount(tallyVO.getBrandCount());
                // 待提交审核品牌
                tallyBO.setToBeBrandValifyCommitCount(tallyVO.getToBeBrandValifyCommitCount());
                // 待审核品牌
                tallyBO.setToBeBrandValifyCount(tallyVO.getToBeBrandValifyCount());
            }

            p.getTallyReport().setCommodity(tallyBO);
        });

        return finalReportList;
    }

    /**
     * 统计商品
     * @param reportList
     * @return
     */
    private List<MemberTallyReportDO> statisticsCommodityTally(List<MemberTallyReportDO> reportList) {
        if (reportList == null) {
            reportList = new ArrayList<>();
        }
        List<CommodityStatisticsTallyBO> statisticsList = commodityDAO.getStatisticsCount();
        if (statisticsList == null) {
            logger.info("没有符合条件的商品计数统计数据");
            statisticsList = new ArrayList<>();
        }

        // 补全统计数据
        List<MemberTallyReportDO> finalReportList = reportList;
        statisticsList.stream().distinct().forEach(p -> {
            MemberTallyReportDO reportDO = finalReportList.stream()
                    .filter(o -> o.getMemberId().equals(p.getMemberId()) && o.getRoleId().equals(p.getRoleId()))
                    .findFirst().orElse(null);
            if (reportDO == null) {
                reportDO = new MemberTallyReportDO();
                reportDO.setMemberId(p.getMemberId());
                reportDO.setRoleId(p.getRoleId());
                finalReportList.add(reportDO);
            }
        });

        List<CommodityStatisticsTallyBO> finalStatisticsList = statisticsList;
        finalReportList.forEach(p -> {
            // 如果报表数据为null，则实例化
            if (p.getTallyReport() == null) {
                p.setTallyReport(new MemberTallyReportBO());
            }
            CommodityTallyBO tallyBO = new CommodityTallyBO();
            CommodityStatisticsTallyBO tallyVO = finalStatisticsList.stream().filter(o -> o.getMemberId().equals(p.getMemberId()) && o.getRoleId().equals(p.getRoleId())).findFirst().orElse(null);
            if (null != tallyVO) {
                // 商品总数
                tallyBO.setProductCount(tallyVO.getProductCount());
                // 待提交审核商品
                tallyBO.setToBeProductValifyCommitCount(tallyVO.getToBeProductValifyCommitCount());
                // 待上架商品数
                tallyBO.setToBeProductPublishCount(tallyVO.getToBeProductPublishCount());
                // 待审核商品
                tallyBO.setToBeProductValifyCount(tallyVO.getToBeProductValifyCount());
            }
            p.getTallyReport().setCommodity(tallyBO);
        });

        return finalReportList;
    }
}
